package ar.edu.unicen.exa.isistan.archassis.view.dialogs.sourcepanel;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import ar.edu.unicen.exa.isistan.archassis.rcs.IVersion;
import ar.edu.unicen.exa.isistan.archassis.rcs.svn.SVNRevisionControlSystem;
import ar.edu.unicen.exa.isistan.archassis.rcs.svn.SVNVersion;
import ar.edu.unicen.exa.isistan.archassis.source.ISourceStrategy;
import ar.edu.unicen.exa.isistan.archassis.source.RCSSourceStrategy;
import ar.edu.unicen.exa.isistan.archassis.view.LongOperations;

public class SVNSourceStrategyPanel extends AbstractSourceStrategyPanel {

	private Text repositorioText;
	//private Button cancelButton;
//	private Button btnNewButton;
	private Table table;
	private Label statusLabel;
	
	private TableColumn commentColumn;
	private TableColumn idColumn;
	private TableColumn userColumn;
	private TableColumn dateColumn;
	
	public SVNSourceStrategyPanel(Composite parent) {
		super(parent);
		this.setLayout(new FormLayout());
		Group repositorioGroup = new Group(this, SWT.NONE);
		repositorioGroup.setText("Repositorio");
		repositorioGroup.setLayout(new FillLayout());
		
		{//Ubico el group
			FormData formData = new FormData();
			formData.right = new FormAttachment(100, -5);
			formData.top = new FormAttachment(0, 5);
			formData.left = new FormAttachment(0, 5);
			repositorioGroup.setLayoutData(formData);
		}
		
		repositorioText = new Text(repositorioGroup, SWT.BORDER);

		repositorioText.setText("http://svn.codespot.com/a/eclipselabs.org/svn-human-diff/trunk/ar.edu.unicen.exa.isistan.plugin.command/");
		repositorioText.setBounds(0, 0, 75, 27);
		
		Composite composite_2 = new Composite(this, SWT.NONE);
		{//Ubico el panel
			FormData formData = new FormData();
			formData.right = new FormAttachment(100, -5);
			formData.bottom = new FormAttachment(100, -5);
			composite_2.setLayoutData(formData);
		}
		{//defino mejor el fillLayout
			FillLayout fillLayout = new FillLayout(SWT.HORIZONTAL);
			fillLayout.spacing = 5;
			composite_2.setLayout(fillLayout);
		}
		
//		cancelButton = new Button(composite_2, SWT.NONE);
//		cancelButton.addSelectionListener(new SelectionAdapter() {
//			@Override
//			public void widgetSelected(SelectionEvent e) {
//				exitAction(false);
//			}
//		});
//		cancelButton.setText("Cancelar");
		
//		btnNewButton = new Button(composite_2, SWT.NONE);
//		btnNewButton.addSelectionListener(new SelectionAdapter() {
//			@Override
//			public void widgetSelected(SelectionEvent e) {
//				okAction();
//			}
//		});
//		btnNewButton.setText("Aceptar");
		
		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);//| SWT.CHECK
		{//Ubico la tabla
			FormData formData = new FormData();
			formData.bottom = new FormAttachment(100, -58);
			formData.top = new FormAttachment(repositorioGroup, 5);
			formData.left = new FormAttachment(0, 5);
			formData.right = new FormAttachment(100, -5);
			table.setLayoutData(formData);
		}
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		statusLabel = new Label(this, SWT.NONE);
		{
			FormData formData = new FormData();
			formData.top = new FormAttachment(table, 6);
			formData.left = new FormAttachment(0, 5);
			formData.right = new FormAttachment(100, -5);
			statusLabel.setLayoutData(formData);
		}
		statusLabel.setText("Status....");
		
		table.addListener(SWT.Selection, new Listener() {
		      public void handleEvent(Event event) {
		    	  tableSelectChange();
//		        String string = event.detail == SWT.CHECK ? "Checked"
//		            : "Selected";
//		        System.out.println(event.item + " " + string);
		      }
		    });
		
		idColumn = new TableColumn(table, SWT.NONE);
		idColumn.setWidth(155);
		idColumn.setText("id");
		
		userColumn = new TableColumn(table, SWT.NONE);
		userColumn.setWidth(100);
		userColumn.setText("Usuario");
		
		dateColumn = new TableColumn(table, SWT.NONE);
		dateColumn.setWidth(100);
		dateColumn.setText("Fecha");
		
		commentColumn = new TableColumn(table, SWT.NONE);
		commentColumn.setWidth(100);
		commentColumn.setText("Comentario");
		
//		cancelButton.setFocus();
		
		repositorioText.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				urlFocusLost();
			}
		});
	}
	
	protected ISourceStrategy okAction() {
		int[] indexes = table.getSelectionIndices();
		if (indexes.length == 0) {
			return null;
		}
		int v0 = indexes[0];
		int v1 = indexes[indexes.length-1];
		
		SVNVersion vv0 = (SVNVersion)table.getItem(v0).getData();
		SVNVersion vv1 = (SVNVersion)table.getItem(v1).getData();
		
		String svnURL = repositorioText.getText();
		
		try {
			SVNRevisionControlSystem svnRCS = new SVNRevisionControlSystem(svnURL);
			
			ISourceStrategy ss = new RCSSourceStrategy(svnRCS, vv0, vv1);
			//ModelManager.getInstance().setSourceStrategy(ss);
			return ss;
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		//table.get
		//exitAction(true);
	}

	protected void tableSelectChange() {
		int[] indexes = table.getSelectionIndices();
		int v0 = indexes[0];
		int v1 = indexes[indexes.length-1];
		
		SVNVersion vv0 = (SVNVersion)table.getItem(v0).getData();
		SVNVersion vv1 = (SVNVersion)table.getItem(v1).getData();
		
		statusLabel.setText("Se comparará " + vv0.getID() + " con " + vv1.getID());
	}
	
	protected void urlFocusLost() {
		
		String svnURL = repositorioText.getText();
		table.clearAll();
		table.setItemCount(0);
		try {
			SVNRevisionControlSystem svnRCS = new SVNRevisionControlSystem(svnURL);
			List<IVersion> versionList = LongOperations.getVersions(this.getShell(), svnRCS);
			//List<IVersion> versionList = PersistenceManager.getCacheVersions(svnRCS);
			
			for (IVersion v : versionList) {
				SVNVersion vv = (SVNVersion)v;
				TableItem tableItem_x = new TableItem(table, SWT.NONE);
				tableItem_x.setText(new String[] {vv.getID(), vv.getAuthor(), vv.getDate(), vv.getLog()});
				tableItem_x.setData(vv);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	public Label getStatusLabel() {
		return statusLabel;
	}

	@Override
	public ISourceStrategy getSourceStrategy() {
		return okAction();
	}

}
